package com.lianxi.service.serviceImpl;

import com.lianxi.mapper.ArticleMapper;
import com.lianxi.mapper.CategoryMapper;
import com.lianxi.pojo.Article;
import com.lianxi.pojo.Category;
import com.lianxi.service.CategoryService;
import com.lianxi.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yaml.snakeyaml.events.Event;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public Boolean addCategory(String cateName) {
        Category cateByName = categoryMapper.getCateByName(cateName);
        if (cateByName==null){
            Category category=new Category(null,cateName,DateUtil.getTime(),null);
            categoryMapper.addCategory(category);
            return true;
        }
        return false;
    }

    @Override
    public int deleCateById(int cid) {
        List<Article> artByCids = articleMapper.getAllstateBycid(cid);
        System.out.println(artByCids);
        if (artByCids!=null&&artByCids.size()>0){
            return -1;
        }else {
            return categoryMapper.deleCateById(cid);
        }
    }

    @Override
    public Category getCateById(int cid) {
        return categoryMapper.getCateById(cid);
    }

    @Override
    public Category getCateByName(String cateName) {
        return categoryMapper.getCateByName(cateName);
    }

    @Override
    public Map<String,Object> getAllCate(String cateName,int current, int size) {
        List<Category> cateArtNum = categoryMapper.getCateArtNum();
        for (Category category : cateArtNum) {
            System.out.println(category.getId()+"@@@"+category.getTotal());
        }
        HashMap<String,Object> map=new HashMap<>();
        int begin=(current-1)*size;
        List<Category> allCates = categoryMapper.getAllCate(cateName,begin, size);
        Long total = categoryMapper.coutAllCate(cateName);
        map.put("allCates",allCates);
        map.put("total",total);
        return map;
    }


    @Override
    public List<Category> getLikeCate(String cateName) {
        return categoryMapper.getLikeCate(cateName);
    }


    //首先判断数据库中有重名的，没有在继续修改
    @Override
    public boolean updaCateName(int cid, String newCateName) {
        Category cate = categoryMapper.getCateByName(newCateName);
        if(cate!=null){
            if(cate.getId()==cid){
                categoryMapper.updaCateName(cid,newCateName);
                return true;
            }else {
                return false;
            }
        }
        categoryMapper.updaCateName(cid,newCateName);
        return true;
    }

    @Override
    public List<Category> getCateArtNum() {
        return categoryMapper.getCateArtNum();
    }
}
